import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.JButton;
import javax.swing.JComponent;

public class TradeDisplay extends JComponent {
	/**
	 * first test button
	 * 
	 * @author ptanaka
	 * 
	 */
	class AcceptTrade extends JButton implements ActionListener {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		/**
		 * this should be default in all buttons we make
		 */
		AcceptTrade() {
			super("Accept");
			addActionListener(this);

		}

		/**
		 * what the button will do when it is clicked
		 */
		public void actionPerformed(ActionEvent arg0) {
			ResourceType[] Resources = new ResourceType[5];
			TradeDisplay.this.Ask.keySet().toArray(Resources);
			if (!TradeDisplay.this.isplayerTrading) {
				for (int i = 0; i < 5; i++) {
					TradeDisplay.this.Receiving.addResource(Resources[i],
							TradeDisplay.this.Ask.get(i));
					TradeDisplay.this.Receiving.removeResoruce(Resources[i],
							TradeDisplay.this.Offer.get(i));
				}
			} else {
				for (int i = 0; i < 5; i++) {
					TradeDisplay.this.Offering.addResource(Resources[i],
							TradeDisplay.this.Offer.get(i));
					TradeDisplay.this.Offering.removeResoruce(Resources[i],
							TradeDisplay.this.Ask.get(i));
				}
			}
			System.exit(0);
		}
	}

	class BankTrade extends JButton implements ActionListener {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		/**
		 * this should be default in all buttons we make
		 */
		BankTrade() {
			super("Trade with bank");
			addActionListener(this);

		}

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

		}
	}

	class CancelTrade extends JButton implements ActionListener {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		/**
		 * this should be default in all buttons we make
		 */
		CancelTrade() {
			super("Cancel");
			addActionListener(this);

		}

		/**
		 * what the button will do when it is clicked
		 */
		public void actionPerformed(ActionEvent arg0) {
			// What the button will do goes here
			System.exit(0);
		}
	}

	class DeclineTrade extends JButton implements ActionListener {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		/**
		 * this should be default in all buttons we make
		 */
		DeclineTrade() {
			super("Decline");
			addActionListener(this);

		}

		/**
		 * what the button will do when it is clicked
		 */
		public void actionPerformed(ActionEvent arg0) {
			System.exit(0);
		}
	}

	// begin trade display
	private JButton DeclineButton = new DeclineTrade();
	private JButton AcceptButton = new AcceptTrade();
	private JButton CancelButton = new CancelTrade();
	private JButton BankButton = new BankTrade();
	private boolean isplayerTrading;
	private Hashtable<ResourceType, Integer> Offer;
	private Hashtable<ResourceType, Integer> Ask;
	private Player Offering;
	private Player Receiving;

	/**
	 * constructs a new trade display
	 * 
	 * @param is
	 *            the player who constructed the display the one who began the
	 *            trade
	 * @param offer
	 *            the cards the player is offering to give
	 * @param ask
	 *            the cards the player is asking for
	 */
	public TradeDisplay(Player guyontrade, boolean isplayerTrading,
			Hashtable<ResourceType, Integer> offer,
			Hashtable<ResourceType, Integer> ask) {
		this.Offer = offer;
		this.Ask = ask;
		if (isplayerTrading) {
			this.Offering = guyontrade;
		} else {
			this.Receiving = guyontrade;
		}
		this.isplayerTrading = isplayerTrading;// !important that this statement
		// is before addbuttons
		addButtons();
	}

	/**
	 * adds all the buttons in the trade panel
	 */
	private void addButtons() {
		if (!this.isplayerTrading) {
			add(this.AcceptButton);
			this.AcceptButton.setBounds(50, 50, 80, 30);
			add(this.DeclineButton);
			this.DeclineButton.setBounds(150, 50, 80, 30);
		} else {
			add(this.CancelButton);
			this.CancelButton.setBounds(50, 50, 80, 30);
			if ((this.Offer.size() >= 4) && isAllSame(this.Offer)
					&& (this.Ask.size() == 1)) {
				add(this.BankButton);
				this.BankButton.setBounds(150, 50, 130, 30);
			}
		}

	}

	/**
	 * this should be used to draw all the cards that the player will trade
	 * 
	 * @param g2
	 *            graphics object to draw
	 */
	public void drawCards(Graphics2D g2) {
		// draw the cards here
		// Rectangle wood = new Rectangle(100, 200, 600, 800);
		// g2.setColor(Color.BLUE);
		// g2.fill(wood);
		// g2.draw(wood);
		g2.drawString("ASKING", 100, 200);
		g2.drawString("OFFERING", 100, 320);
		g2.drawString(this.Offer.elements().nextElement().toString(), 200, 220);
		g2.drawString(this.Offer.elements().nextElement().toString(), 200, 240);
		g2.drawString(this.Offer.elements().nextElement().toString(), 200, 260);
		g2.drawString(this.Offer.elements().nextElement().toString(), 200, 280);
		g2.drawString(this.Offer.elements().nextElement().toString(), 200, 300);
		// ----
		g2.drawString(this.Offer.elements().nextElement().toString(), 100, 340);
		g2.drawString(this.Offer.elements().nextElement().toString(), 100, 360);
		g2.drawString(this.Offer.elements().nextElement().toString(), 100, 380);
		g2.drawString(this.Offer.elements().nextElement().toString(), 100, 400);
		g2.drawString(this.Offer.elements().nextElement().toString(), 100, 420);

	}

	private boolean isAllSame(Hashtable<ResourceType, Integer> offer) {
		Integer[] totalnum = new Integer[5];
		offer.values().toArray(totalnum);
		boolean zero = true;
		for (Integer integer : totalnum) {
			if ((integer != 0) && !zero) {
				return false;
			} else if (integer != 0) {
				zero = false;
			}
		}
		return true;
	}
}
